import 'package:flutter/material.dart';
import 'package:flutter_common_app/api/get_calendar_api.dart';
import 'package:flutter_common_app/data/calendar_data.dart';
import 'package:flutter_common_app/router/route_util.dart';
import 'package:flutter_common_app/utils/common_page_load_controller.dart';
import 'package:flutter_common_app/utils/common_util/timeline_util.dart';
import 'package:flutter_common_app/widgets/custom_image/custom_image.dart';
import 'package:flutter_common_app/widgets/custom_list/custom_loading_more_list.dart';
import 'package:flutter_common_app/widgets/empty_view/h_empty_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class SongCalendarTabView extends StatefulWidget {
  final DateTime dateTime;

  const SongCalendarTabView(this.dateTime, {Key key}) : super(key: key);

  @override
  _SongCalendarTabViewState createState() => _SongCalendarTabViewState();
}

class _SongCalendarTabViewState extends State<SongCalendarTabView>
    with AutomaticKeepAliveClientMixin {
  CommonPageLoadController<CalendarEventsData> loadController;

  @override
  void initState() {
    int startStamp = widget.dateTime.millisecondsSinceEpoch;
    int endStamp = DateTime(widget.dateTime.year, widget.dateTime.month + 1, 1)
        .millisecondsSinceEpoch;
    GetCalendarApi getCalendarApi = GetCalendarApi(context)
      ..getParams()['startTime'] = startStamp
      ..getParams()['endTime'] = endStamp
      ..fromResponseData = (res) {
        Map json = {'code': res['code'], 'data': res['data']['calendarEvents']};
        return json;
      };

    loadController = CommonPageLoadController<CalendarEventsData>(
        getCalendarApi, (jsonList) {
      return CalendarEventsData.fromJsonList(jsonList);
    });

    super.initState();
  }

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return CustomLoadingMoreList(
      (BuildContext context, CalendarEventsData data, int index) {
        return GestureDetector(
          onTap: () {
            if (data.resourceType == 'WEBVIEW') {
              RouteUtil.toWebViewPage(
                context,
                data.resourceId,
              );
            }
          },
          child: listItemWidget(data),
        );
      },
      loadController,
    );
  }

  Widget listItemWidget(CalendarEventsData data) {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 15.w, horizontal: 30.w),
      padding: EdgeInsets.symmetric(vertical: 30.w, horizontal: 30.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(20.w),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            TimelineUtil.todayBeforeAndAfter(data.onlineTime),
            style: TextStyle(
              color: Colors.black45,
              fontSize: 30.sp,
            ),
          ),
          Row(
            children: [
              Expanded(
                child: Text(
                  data.title,
                  style: TextStyle(
                    fontSize: 30.sp,
                  ),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              HEmptyView(20.w),
              CustomImage.network(
                data.imgUrl + '?param=120y120',
                width: 120.w,
                height: 120.w,
                borderRadius: BorderRadius.all(
                  Radius.circular(20.w),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
